package org.vectrics.recruiter.reporting;

import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.VectricsException;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.recruiter.user.InterviewCriteria;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.skill.Skill;
import org.vectrics.skill.SkillProvider;
import org.vectrics.skill.SkillRatingSet;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.user.ContactInfo;
import org.vectrics.user.Person;



public class CandidateReport extends RecruitBaseReport {
	private transient static Logger log = Logger.getLogger(CandidateReport.class);

	private PdfPTable mainTable = null;
	private String personId = null;
	private PdfPTable coreTable = null;
	
	public CandidateReport(Domain domain, Locale locale) {
		super(domain, locale);
	}

	@Override
	protected Rectangle getPageSize() {
		return(PageSize.A4);
	}

	@Override
	protected void fill(Document document) throws VectricsException {
		log.debug("Candidate Report - building");
		try {
			Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(this.personId));
			if (person == null) {
				log.error("Candidate not found for personId = " + this.personId);
				throw new VectricsException("Person not found for personId = " + this.personId);
			}
			CandidateInfo candidate = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(person);
			
			document.add(new Paragraph(" ", ReportConstants.heading1Font));
			document.add(new Paragraph(" ", ReportConstants.heading1Font));
			document.add(new Paragraph(" ", ReportConstants.heading1Font));
			String documentTitle = this.getPhrase("report.candidate.title");
			document.add(new Paragraph(documentTitle, ReportConstants.heading1Font));
			document.add(new Paragraph(" ", ReportConstants.heading1Font));
			document.add(new Paragraph(person.getName(), ReportConstants.heading3Font));
			//document.add(new Paragraph(job.getReferenceId(), ReportConstants.heading3Font));
			
			
			document.add(new Paragraph("", ReportConstants.titleFont));
			this.coreTable = new PdfPTable(2);
			coreTable.setHorizontalAlignment(Element.ALIGN_LEFT);
			coreTable.setWidthPercentage(60);
			int coreTableWidths[] = {200, 200};
			coreTable.setWidths(coreTableWidths);
			
			showPersonInformation(person);

			showCandidateInformation(candidate);

			Chapter chapter = this.createChapter("Candidate Information");
			chapter.add(coreTable);

			document.add(chapter);
			if (candidate !=  null) {
				InterviewCriteria interviewCriteria = new InterviewCriteria();
				interviewCriteria.setPerson(person);
				//interviewCriteria.
				Collection interviews = RecruitServiceLocator.getRecruitUserService().findInterviews(interviewCriteria, 0, 10000);
				
				
				Iterator interviewIter = interviews.iterator();
				showInterviews(interviewIter);
				
				Iterator resumeIter = candidate.getResumes().iterator();
				showResumes(resumeIter);
			}
		}
		catch(Exception e) {
			log.error("Exception creating pdf document - in fill()", e);
			throw new VectricsException("Exception creating pdf document - fill()", e);
		}
        
	}
	
	
	protected void setupHeaderFooter(Document document) throws VectricsException {
	}

	
	protected void showPersonInformation(Person person) {
		addCoreRow(coreTable, "Candidate", person.getName());
		addCoreRow(coreTable, "Gender", person.getGender());
		SimpleDateFormat dateFormat = CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale());
		addCoreRow(coreTable, "Created", dateFormat.format(person.getCreatedDate()));
		if (person.getDateOfBirth() != null) 
			addCoreRow(coreTable, "Date of Birth", dateFormat.format(person.getDateOfBirth()));
		else
			addCoreRow(coreTable, "Date of Birth", null);

		if (person.getContactInfo() != null) {
			ContactInfo contactInfo = person.getContactInfo();
			addCoreRow(coreTable, "E-Mail", contactInfo.getEmail());
			addCoreRow(coreTable, "E-Mail 2", contactInfo.getEmail2());
			if (contactInfo.getHomePhone() != null)
				addCoreRow(coreTable, "Home Phone", contactInfo.getHomePhone().getFormattedNumber());
			if (contactInfo.getWorkPhone() != null)
				addCoreRow(coreTable, "Work Phone", contactInfo.getWorkPhone().getFormattedNumber());
			if (contactInfo.getCellPhone() != null)
				addCoreRow(coreTable, "Cell Phone", contactInfo.getCellPhone().getFormattedNumber());
		}
	}

	
	protected void showCandidateInformation(CandidateInfo candidate) {
		if (candidate == null) {
			addCoreRow(coreTable, "Candidate Information", null);
		} else {
			addCoreRow(coreTable, "Career Goals", candidate.getCareerGoals());
			addCoreRow(coreTable, "Currently Working", "" + candidate.getCurrentlyWorking());
			
			if (candidate.getExpectedSalary() != null)
				addCoreRow(coreTable, "Expected Salary", candidate.getExpectedSalary().toString());
			else
				addCoreRow(coreTable, "Expected Salary", null);
				
			if (candidate.getHighestEducation() != null)
				addCoreRow(coreTable, "Highest Education", this.getPhrase(candidate.getHighestEducation().getDescriptionCode()));
			else
				addCoreRow(coreTable, "Highest Education", null);

			int numberOfJobAgents = candidate.getJobAgents().size();
			addCoreRow(coreTable, "Number of Job Agents", "" + numberOfJobAgents);
			
			int numberOfResumes = candidate.getResumes().size();
			addCoreRow(coreTable, "Number of Resumes", "" + numberOfResumes);
	
			if (candidate.getTravelPercent() != null)
				addCoreRow(coreTable, "Acceptable Travel", candidate.getTravelPercent().toString() + "%");
			else
				addCoreRow(coreTable, "Acceptable Travel", null);
			
		}
	}
	
	protected void showResumes(Iterator resumeIter) {
		try {
			Chapter chapter = this.createChapter("Candidate Resumes");

			int infoTableWidths[] = {80, 120, 120};
			PdfPTable infoTable = new PdfPTable(infoTableWidths.length);
			infoTable.setHorizontalAlignment(Element.ALIGN_LEFT);
			infoTable.setWidthPercentage(100);
			infoTable.setWidths(infoTableWidths);
	
			
			PdfPCell dateCell = new PdfPCell(new Phrase(this.getPhrase("prompt.resume.date"), ReportConstants.stdBoldFont));
			dateCell.setBorderWidth(1);
			dateCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			infoTable.addCell(dateCell);
			
			PdfPCell resumeNameCell = new PdfPCell(new Phrase(this.getPhrase("prompt.resume.name"), ReportConstants.stdBoldFont));
			resumeNameCell.setBorderWidth(1);
			resumeNameCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			infoTable.addCell(resumeNameCell);
			
			PdfPCell fileTypeCell = new PdfPCell(new Phrase(this.getPhrase("prompt.resume.type"), ReportConstants.stdBoldFont));
			fileTypeCell.setBorderWidth(1);
			fileTypeCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			infoTable.addCell(fileTypeCell);
			
			if (resumeIter.hasNext() == false) {
				PdfPCell cell = new PdfPCell(new Phrase("No Resumes Submitted", ReportConstants.stdFont));
				cell.setBorderWidth(1);
				cell.setColspan(infoTableWidths.length);
				infoTable.addCell(cell);
			}

			while (resumeIter.hasNext()) {
				Resume resume = (Resume)resumeIter.next();

				SimpleDateFormat dateFormat = CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale());
				PdfPCell cell = new PdfPCell(new Phrase(dateFormat.format(resume.getLastUpdate()), ReportConstants.stdFont));
				cell.setBorderWidth(1);
				infoTable.addCell(cell);

				cell = new PdfPCell(new Phrase(resume.getName(), ReportConstants.stdFont));
				cell.setBorderWidth(1);
				infoTable.addCell(cell);

				cell = new PdfPCell(new Phrase(resume.getDocument().getFileExtension(), ReportConstants.stdFont));
				cell.setBorderWidth(1);
				infoTable.addCell(cell);
				
				
			}
			
			chapter.add(infoTable);
			this.getDocument().add(chapter);

		} catch (Exception ex) {
			throw new SystemException("Exception showing resumes", ex);
		}
	}

	protected void showInterviews(Iterator interviewIter) {
		try {
			Chapter chapter = this.createChapter("Candidate Interviews");

			int infoTableWidths[] = {80, 120, 120, 120};
			PdfPTable infoTable = new PdfPTable(infoTableWidths.length);
			infoTable.setHorizontalAlignment(Element.ALIGN_LEFT);
			infoTable.setWidthPercentage(100);
			infoTable.setWidths(infoTableWidths);
	
			
			PdfPCell dateCell = new PdfPCell(new Phrase(this.getPhrase("prompt.interview.date"), ReportConstants.stdBoldFont));
			dateCell.setBorderWidth(1);
			dateCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			infoTable.addCell(dateCell);
			
			PdfPCell resumeNameCell = new PdfPCell(new Phrase(this.getPhrase("prompt.interview.interviewer"), ReportConstants.stdBoldFont));
			resumeNameCell.setBorderWidth(1);
			resumeNameCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			infoTable.addCell(resumeNameCell);
			
			PdfPCell fileTypeCell = new PdfPCell(new Phrase(this.getPhrase("prompt.interview.status"), ReportConstants.stdBoldFont));
			fileTypeCell.setBorderWidth(1);
			fileTypeCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			infoTable.addCell(fileTypeCell);
			
			PdfPCell resultCell = new PdfPCell(new Phrase(this.getPhrase("prompt.interview.result"), ReportConstants.stdBoldFont));
			resultCell.setBorderWidth(1);
			resultCell.setBackgroundColor(ReportConstants.TABLE_LABEL_COLOR);
			infoTable.addCell(resultCell);
			
			if (interviewIter.hasNext() == false) {
				PdfPCell cell = new PdfPCell(new Phrase("No Interviews Scheduled nor Completed", ReportConstants.stdFont));
				cell.setBorderWidth(1);
				cell.setColspan(infoTableWidths.length);
				infoTable.addCell(cell);
			}

			while (interviewIter.hasNext()) {
				Interview interview = (Interview)interviewIter.next();

				SimpleDateFormat dateFormat = CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale());
				PdfPCell cell = new PdfPCell(new Phrase(dateFormat.format(interview.getDateStart()), ReportConstants.stdFont));
				cell.setBorderWidth(1);
				infoTable.addCell(cell);

				cell = new PdfPCell(new Phrase(interview.getLeadPerson().getName(), ReportConstants.stdFont));
				cell.setBorderWidth(1);
				infoTable.addCell(cell);

				cell = new PdfPCell(new Phrase(interview.getStatusType().getName(), ReportConstants.stdFont));
				cell.setBorderWidth(1);
				infoTable.addCell(cell);

				if (interview.getResultType() != null) {
					cell = new PdfPCell(new Phrase(this.getPhrase(interview.getResultType().getDescriptionCode()), ReportConstants.stdFont));
				} else {
					cell = new PdfPCell(new Phrase("", ReportConstants.stdFont));
				}
				cell.setBorderWidth(1);
				infoTable.addCell(cell);
			}
			
			chapter.add(infoTable);
			this.getDocument().add(chapter);

		} catch (Exception ex) {
			throw new SystemException("Exception showing resumes", ex);
		}
	}
	
	
	protected void addTableHeaderCell(String title) {
		log.debug("addTableHeaderCell()");
		PdfPCell hdrCell = new PdfPCell(new Phrase(title, ReportConstants.stdBoldFont));
		hdrCell.setBackgroundColor(ReportConstants.TABLE_HEAD_COLOR);
		this.mainTable.addCell(hdrCell);	
	}
	   

	public String getPersonId() {
		return personId;
	}

	public void setPersonId(String personId) {
		this.personId = personId;
	}

	
	private void addCoreRow(PdfPTable coreTable, String caption, String value) {
		PdfPCell captionCell = new PdfPCell(new Phrase(caption, ReportConstants.stdBoldFont));
		captionCell.setBorderWidth(1);
		coreTable.addCell(captionCell);
		String valueStr = value;
		if ((valueStr == null) || (valueStr.length() <= 0)) {
			valueStr = this.getPhrase("phrase.not.given");
		}
		PdfPCell valueCell = new PdfPCell(new Phrase(valueStr, ReportConstants.stdFont));
		valueCell.setBorderWidth(1);
		valueCell.setPaddingBottom(5);
		coreTable.addCell(valueCell);
	}
	
}
